/*
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
 * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
 * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

package com.swak.monitor.dubbo.web;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.swak.annotation.Delete;
import com.swak.annotation.Get;
import com.swak.annotation.Post;
import com.swak.annotation.Put;
import com.swak.annotation.RestApi;
import com.swak.monitor.dubbo.exception.ParamValidationException;
import com.swak.monitor.dubbo.exception.ResourceNotFoundException;
import com.swak.monitor.dubbo.model.dto.TagRouteDTO;
import com.swak.monitor.dubbo.service.RouteService;
import com.swak.monitor.dubbo.utils.Constants;
import com.swak.utils.StringUtils;

@RestApi(value = "DubboTagRoutesApi", path = "/api/monitor/:env/rules/route/tag")
public class TagRoutesApi {

	@Autowired
	private RouteService routeService;
	// @Autowired
	// private ProviderService providerService;

	@Post
	public boolean createRule(TagRouteDTO routeDTO, String env) {
		String app = routeDTO.getApplication();
		if (StringUtils.isEmpty(app)) {
			throw new ParamValidationException("app is Empty!");
		}
//        if (providerService.findVersionInApplication(app).equals("2.6")) {
//            throw new VersionValidationException("dubbo 2.6 does not support tag route");
//        }
		routeService.createTagRoute(routeDTO);
		return true;
	}

	@Put("/:id")
	public boolean updateRule(String id, TagRouteDTO routeDTO, String env) {

		id = id.replace(Constants.ANY_VALUE, Constants.PATH_SEPARATOR);
		// String app = routeDTO.getApplication();
//		if (providerService.findVersionInApplication(app).equals("2.6")) {
//			throw new VersionValidationException("dubbo 2.6 does not support tag route");
//		}
		if (routeService.findTagRoute(id) == null) {
			throw new ResourceNotFoundException("can not find tag route, Id: " + id);
		}
		routeService.updateTagRoute(routeDTO);
		return true;

	}

	@Get
	public List<TagRouteDTO> searchRoutes(String application, String env) {
		if (StringUtils.isBlank(application)) {
			throw new ParamValidationException("application is required.");
		}
		List<TagRouteDTO> result = new ArrayList<>();
//		String version = "2.6";
//		try {
//			version = providerService.findVersionInApplication(application);
//		} catch (ParamValidationException e) {
//			// ignore
//		}
//		if (version.equals("2.6")) {
//			return result;
//		}

		TagRouteDTO tagRoute = routeService.findTagRoute(application);
		if (tagRoute != null) {
			result.add(tagRoute);
		}
		return result;
	}

	@Get("/:id")
	public TagRouteDTO detailRoute(String id, String env) {
		id = id.replace(Constants.ANY_VALUE, Constants.PATH_SEPARATOR);
		TagRouteDTO tagRoute = routeService.findTagRoute(id);
		if (tagRoute == null) {
			throw new ResourceNotFoundException("Unknown ID!");
		}
		return tagRoute;
	}

	@Delete("/:id")
	public boolean deleteRoute(String id, String env) {
		id = id.replace(Constants.ANY_VALUE, Constants.PATH_SEPARATOR);
		routeService.deleteTagRoute(id);
		return true;
	}

	@Put("/enable/:id")
	public boolean enableRoute(String id, String env) {
		id = id.replace(Constants.ANY_VALUE, Constants.PATH_SEPARATOR);
		routeService.enableTagRoute(id);
		return true;
	}

	@Put("/disable/:id")
	public boolean disableRoute(String id, String env) {
		id = id.replace(Constants.ANY_VALUE, Constants.PATH_SEPARATOR);
		routeService.disableTagRoute(id);
		return true;
	}
}
